﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Database;
using System.Data;
using ShoppingCart;

public partial class index : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        getNewProduct();
    }
    public void getNewProduct()
    {
        MayTinh mt = new MayTinh();
        DataTable dt = mt.DSMTBC();
        //DataList1.DataSource = ds;
        //DataList1.DataBind();
        DataSet ds = new DataSet();
        ds.Tables.Add(dt);
        PagedDataSource objPds = new PagedDataSource();
        ///DataSet ds = lk.DSLinhKien();
        objPds.DataSource = ds.Tables[0].DefaultView;// ds.DefaultViewManager;
        objPds.AllowPaging = true;
        objPds.PageSize = 2;
        objPds.CurrentPageIndex = this.currentPage;
        lblCurrentPage.Text = "Page: " + (this.currentPage + 1).ToString() + " of " + objPds.PageCount.ToString();
        cmdPrev.Enabled = !objPds.IsFirstPage;
        cmdNext.Enabled = !objPds.IsLastPage;
        DataList1.DataSource = objPds;
        DataList1.DataBind();
    }
    protected void cmdPrev_Click(object sender, System.EventArgs e)
    {
        currentPage -= 1;
        getNewProduct();
    }

    protected void cmdNext_Click(object sender, System.EventArgs e)
    {
        currentPage += 1;
        getNewProduct();

    }

    public int currentPage
    {
        get
        {
            object o = this.ViewState["_CurrentPage"];
            if (o == null)
                return 0; // default to showing the first page
            else
                return (int)o;
        }
        set
        {
            this.ViewState["_CurrentPage"] = value;
        }
    }
    protected void DataList1_ItemCommand(object sender, DataListCommandEventArgs e)
    {
        if (e.CommandName == "Mua")
        {
            int ma = Convert.ToInt32(e.CommandArgument.ToString());
            MayTinh mt = new MayTinh();
            DataTable product = mt.MayTinhTheoMa(ma);
            Item item = new Item();
            DataRow row = product.Rows[0];

            item.ID = Convert.ToInt32(row["mamay"].ToString());
            item.Name = row["tenmay"].ToString();
            item.Cost = Convert.ToDouble(row["giaban"].ToString());
            item.Quantity = 1;


            if (Session["ShoppingCart"] == null)
            {
                Session["ShoppingCart"] = new ShoppingCart.Cart();
            }

            ShoppingCart.Cart cart = (ShoppingCart.Cart)Session["ShoppingCart"];
            cart.AddToCart(item);
            Session["ShoppingCart"] = cart;
            string links = Request.ServerVariables["HTTP_REFERER"].ToString();
            Response.Redirect(links);
        }
    }
}